package cn.ilovese.collect.serviceimpl.follow;

import cn.ilovese.collect.controller.socket.WebSocketServer;
import cn.ilovese.collect.datamapper.follow.FollowMapper;
import cn.ilovese.collect.datamapper.user.UserMapper;
import cn.ilovese.collect.datamapper.workermessage.WorkerMessageMapper;
import cn.ilovese.collect.po.follow.Follow;
import cn.ilovese.collect.po.user.User;
import cn.ilovese.collect.po.workermessage.WorkerMessage;
import cn.ilovese.collect.service.follow.FollowService;
import cn.ilovese.collect.util.Constant;
import cn.ilovese.collect.vo.ResultVO;
import cn.ilovese.collect.vo.follow.FollowGroupVO;
import cn.ilovese.collect.vo.user.UserVO;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@CacheConfig(cacheNames = "follow",cacheManager = "cacheManager")
public class FollowServiceImpl implements FollowService {
    @Resource
    FollowMapper followMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    WorkerMessageMapper workerMessageMapper;

    @Override
    public ResultVO<Integer> batchFollow(FollowGroupVO followGroupVO) {
        Integer firstId = followGroupVO.getWorkerid();
        List<Integer> followings = followGroupVO.getWorkeridList();
        WorkerMessage message = new WorkerMessage();
        message.setCooReportId(0);
        message.setMessageType(6);
        message.setReportId(1);
        message.setFromRole(1);
        message.setSubmitTime(new Date());
        message.setStatus(0);
        message.setTaskId(1);
        message.setTaskName(" ");
        message.setFromUserId(firstId);
        for(Integer i: followings){
            Follow follow = new Follow();
            follow.setUserId(firstId);
            follow.setFollowingId(i);
            int res = followMapper.insert(follow);
            if(res < 1)
                return new ResultVO<>(Constant.REQUEST_FAIL,"插入失败",-1);
            else {
                message.setToWorkerId(i);
                int messageId = workerMessageMapper.insert(message);
                WebSocketServer.sendMessageToSpecificUser(workerMessageMapper.countStatus(0, message.getToWorkerId()), message.getToWorkerId());
            }
        }
        return new ResultVO<>(Constant.REQUEST_SUCCESS,"插入成功",1);
    }

    @Override
    public ResultVO<Integer> batchUnfollow(FollowGroupVO followGroupVO) {
        Integer firstId = followGroupVO.getWorkerid();
        List<Integer> followings = followGroupVO.getWorkeridList();
        for(Integer i: followings){
            int res = followMapper.deleteByTwoWorker(firstId,i);
            if(res < 1)
                return new ResultVO<>(Constant.REQUEST_FAIL,"删除失败",-1);
            else{
                int messageId = workerMessageMapper.deleteFollowMessage(firstId,i);
                WebSocketServer.sendMessageToSpecificUser(workerMessageMapper.countStatus(0, i), i);
            }
        }
        return new ResultVO<>(Constant.REQUEST_SUCCESS,"删除成功",1);
    }

    @Override
    public ResultVO<List<UserVO>> getFollowingsByWorkerId(Integer workerId) {
        List<Follow> follows = followMapper.selectByFirstId(workerId);
        List<UserVO> users = new ArrayList<>();
        for(Follow follow: follows){
            User user = userMapper.selectByPrimaryKey(follow.getFollowingId());
            if(user == null)
                return new ResultVO<>(Constant.REQUEST_SUCCESS,"没有对应的用户",new ArrayList<>());
            users.add(new UserVO(user));
        }
        return new ResultVO<>(Constant.REQUEST_SUCCESS,"查找成功", users);
    }

    //TODO
    //获取关注了一个工人的工人数目
    public Integer getNumOfFollowers(Integer workerId){
        List<Follow> followers = followMapper.selectBySecondId(workerId);
        return followers.size();
    }
}
